package patrol.mobile.com.mobilepatrol.fragment;

import android.content.Intent;
import android.content.res.Resources;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.dl7.recycler.listener.OnRecyclerViewItemClickListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.bingoogolapple.bgabanner.BGABanner;
import cn.bingoogolapple.bgabanner.BGABannerUtil;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.TextActivtyByZzm;
import patrol.mobile.com.mobilepatrol.activity.TextActivtyByZzm02;
import patrol.mobile.com.mobilepatrol.activity.bridge.BridgeListActivity;
import patrol.mobile.com.mobilepatrol.activity.culvert.CulvertListActivity;
import patrol.mobile.com.mobilepatrol.activity.road_block.RoadBlockActivity;
import patrol.mobile.com.mobilepatrol.activity.road_survey.TrafficInvestActivity;
import patrol.mobile.com.mobilepatrol.activity.route_query.RouteActivity;
import patrol.mobile.com.mobilepatrol.activity.route_query.TotalRouteActivity;
import patrol.mobile.com.mobilepatrol.activity.track.TrackListActivity;
import patrol.mobile.com.mobilepatrol.adapter.FunTypeAdapter;
import patrol.mobile.com.mobilepatrol.bean.FunctionType;
import patrol.mobile.com.mobilepatrol.manager.UserManager;
import patrol.mobile.com.mobilepatrol.utils.CommonUtil;

/**
 * Created by apple on 17/9/29.
 */

public class MainFragment extends BaseFragment {
    @BindView(R.id.recycler)
    RecyclerView recyclerView;
    private BaseQuickAdapter mAdapter;
    private ArrayList<FunctionType> mList = new ArrayList<>();
    private int imgs[] = {R.mipmap.l1,R.mipmap.l2,R.mipmap.l3,R.mipmap.l4,R.mipmap.l5,R.mipmap.l6};
    private BGABanner mBanner;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main2;
    }

    @Override
    protected void initView() {
        super.initView();
        recyclerView.setLayoutManager(new GridLayoutManager(mActivity,3));
        View v = LayoutInflater.from(getActivity()).inflate(R.layout.top_iv,null);
        mBanner = v.findViewById(R.id.banner);
        mAdapter = new FunTypeAdapter(mActivity,mList);
        mAdapter.addHeaderView(v);
        recyclerView.setAdapter(mAdapter);
    }

    @Override
    public void initData() {
        List<View> views = new ArrayList<>();
        views.add(BGABannerUtil.getItemImageView(getActivity(), R.mipmap.pic1));
        views.add(BGABannerUtil.getItemImageView(getActivity(), R.mipmap.pic2));
        mBanner.setData(views);
        Resources res =getResources();
        String[] fun =res.getStringArray(R.array.fun);
        for (int i=0;i<fun.length;i++){
            FunctionType type = new FunctionType();
            type.id = i;
            type.img = imgs[i];
            type.des = fun[i];
            mList.add(type);
        }
       mAdapter.updateItems(mList);

    }

    @Override
    public void initTitle() {
        tv_center_title.setVisibility(View.VISIBLE);
        tv_center_title.setText("首页");
    }

    @Override
    protected void initListener() {
        mAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                switch (position){
                    case 1:
                        startActivity(TrackListActivity.class);
                        CommonUtil.openNewActivityAnim(getActivity(),false);
                        break;
                    case 2:
                        startActivity(TrafficInvestActivity.class);
                        CommonUtil.openNewActivityAnim(getActivity(),false);
                        break;
                    case 3:
                        startActivity(BridgeListActivity.class);
                        CommonUtil.openNewActivityAnim(getActivity(),false);
                        break;
                    case 4:
                        startActivity(CulvertListActivity.class);
                        CommonUtil.openNewActivityAnim(getActivity(),false);
                        break;
                    case 5:
                        startActivity(RoadBlockActivity.class);
                        CommonUtil.openNewActivityAnim(getActivity(),false);
                        break;
                    case 6:
                        if (UserManager.getUser().orgtype.equals("总局")){
                            Intent intent = new Intent(getActivity(),TotalRouteActivity.class);
                            intent.putExtra("from",1);
                            startActivity(intent);
                            CommonUtil.openNewActivityAnim(getActivity(),false);
                        }else{
                            Intent intent = new Intent(getActivity(),RouteActivity.class);
                            intent.putExtra("from",1);
                            startActivity(intent);
                            CommonUtil.openNewActivityAnim(getActivity(),false);
                        }

                        break;
                }
            }
        });
    }
}
